<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/21 0021
 * Time: 22:55
 */

namespace Lib\Core\WebSocket;

/**
 * @description:通过路径触发控制器的方法
 * @auth:yfl
 * @email:554665488@qq.com
 * Class Dispatch
 * @package Lib\Core\WebSocket
 */
class Dispatch
{
    public static function open($server, $request, $path)
    {
        try {
            //通过路径得到类名实例化
            $className = self::getClassName($path);
            if ($className != false) {
                $obj = new $className;
                $obj->open($server, $request);
            }

        } catch (\Throwable $throwable) {
            var_dump($throwable->getMessage());
        }
    }

    public static function message($server, $frame,$path)
    {
        try {
            //通过路径得到类名实例化
            $className = self::getClassName($path);
            if ($className != false) {
                $obj = new $className;
                $obj->message($server, $frame);
            }

        } catch (\Throwable $throwable) {
            var_dump($throwable->getMessage());
        }
    }

    public static function close($server, $fd,$path)
    {
        try {
            //通过路径得到类名实例化
            $className = self::getClassName($path);
            if ($className != false) {
                $obj = new $className;
                $obj->close($server, $fd);
            }

        } catch (\Throwable $throwable) {
            var_dump($throwable->getMessage());
        }
    }

    public static function getClassName($path)
    {
        if (!isset($path)) return false;
        $nameSpace = 'App\WebSocket\\';
//        var_dump($path);
        $className = $nameSpace . ucfirst(strtolower(explode('/', $path)[1]));
        if (class_exists($className)) return $className;
        return false;
    }
}